﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JBeamLibrary.Tools;
using JBeamLibrary.VehicleParts.Interfaces;
using Newtonsoft.Json.Linq;

namespace JBeamLibrary.VehicleParts.Elements
{
    public class Slot : VehicleElement
    {
        #region Fields

        private readonly VehiclePart _parentPart;

        #endregion

        #region Properties

        /// <summary>Gets the slot's type name</summary>
        public string TypeName { get; set; }

        /// <summary>Gets the slot description</summary>
        public string Description { get; set; }

        /// <summary>Gets the slot's parent vehicle part</summary>
        public VehiclePart ParentPart { get { return _parentPart; } }

        /// <summary>Gets or sets the default vehicle part name for this slot</summary>
        public string DefaultPartName { get; set; }

        #endregion

        #region CTORs

        public Slot()
        {
            TypeName = string.Empty;
            Description = string.Empty;
        }

        public Slot(string typeName, string description, VehiclePart parentPart)
        {
            TypeName = typeName;
            Description = description;
            _parentPart = parentPart;

            if (string.IsNullOrEmpty(typeName))
                throw new ArgumentException("A slot's type name must always be defined");
            if (string.IsNullOrEmpty(description))
                throw new ArgumentException("A slot's description must always be defined");
            if (parentPart == null)
                throw new ArgumentNullException(string.Format("A slot must always belong to a VehiclePart (slot {0}, {1})", typeName, description));
        }

        #endregion

        #region Methods

        public override IVehicleElement FromJBeam(JToken token)
        {
            return this;
        }

        public override JToken ToJBeam()
        {
            return new JArray { TypeName, DefaultPartName, Description };
        }

        public override string ToString()
        {
            return string.Format("{0}, {1}, {2}", TypeName, DefaultPartName, Description);
        }

        #endregion
    }
}
